from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import page

class GetDriver:
    driver = None
    #获取driver
    @classmethod
    def get_driver(cls):
        try:
            if cls.driver is None:
                #获取driver
                # 自动下载并配置匹配的ChromeDriver
                # cls.driver = webdriver.Chrome(ChromeDriverManager().install())
                cls.driver = webdriver.Chrome()
                #最大化浏览器
                cls.driver.maximize_window()
                #打开url
                cls.driver.get(page.URL)
            #返回driver
            return cls.driver
        except Exception as e:
            print(f"Error while getting driver :{e}")
            raise
    #关闭driver
    @classmethod
    def quit_driver(cls):
        try:
            if cls.driver:
                #关闭driver
                cls.driver.quit()
                #必须置空
                cls.driver = None
        except Exception as e:
            print(f"Error while quitting driver: {e}")